<?php
namespace mod;
use think\Model;


class User extends Model
{
    protected $readonly = [ 
    	'uid' , 
    	'username' ,
    	'phone' , 
    	'regtime' ,
    	'regip' ,
    	'user_status' ,
    	'lock_end_time' ,
    	'rid' ,
    	'department' ,
    	'manage_city'
    	];

    protected $insert = [ 
    	'username' , 
    	'code' , 
    	'regtime' ,
    	'regip' ,
    	'user_status' ,
    	'lock_end_time' ,
    	'credits' ,
    	'rid' ,
    	'icon' ,
    	'department'
    	];

    protected $update = [];

    public function setUsernameAttr()
    {
        return 'lianpiao_'.time().rand(1,10);
    }

    public function setCodeAttr()
    {
        return substr(md5(mt_rand() . time()), -10);
    }

    public function setRegtimeAttr()
    {
        return time();
    }

    public function setRegipAttr()
    {
        return request()->ip();
    }

    public function setUserStatusAttr()
    {
        return 1;
    }

    public function setLockEndTimeAttr()
    {
        return 0;
    }

    public function setCreditsAttr()
    {
        return 100;
    }

    public function setRidAttr()
    {
        return 5;
    }

    public function setIconAttr()
    {
        return '/Static/image/user.png';
    }

    public function setDepartmentAttr()
    {
        return 0;
    }

    public function setPasswordAttr($value,$data){
    	return md5($value.$data['code']);
    }

    public function setNicknameAttr($value,$data){
		return $data['phone'];
    }


    //会员登录
    public function userLogin( $data , $rid = false )
    {
        $user = self::where("phone",$data['phone'])->find();

        if ( empty($user) ) {
            $this->error = '会员号不存在，请注册后登陆';
            return false;
        }
        if (md5($data['password'] . $user['code']) != $user['password']) {
            $this->error = '密码错误';
            return false;
        }
        $roleResult = db('role')->find($user['rid']);
        if( $rid == 'admin' ){
	        if( $roleResult['admin'] <> 1 ){
		        $this->error = '账号密码错误，警告!';
		        return false;
	        }
        }else{
	        if( $roleResult['admin'] == 1 ){
		        $this->error = '管理员请登录后台面板';
		        return false;
	        }
	        if($rid == "mobile" && $user['open_id'] <> '0'){
		        $this->error = '账号已经绑定过微信号，不能重复登陆';
		        return false;
	        }
        }

        //更新最后登陆时间和ip
	    $user->lastip    = request()->ip();
	    $user->logintime = time();
        $user->save();
        unset($user['password']);
        unset($user['code']);

        $user['lastip']    = request()->ip();
	    $user['logintime'] = time();
        session( 'user' , $user );

        return true;
    }

    /*
     * 会员用户注册
     *
     * @param   array   $data   用户注册信息
     * @param   bool    $flag   是否检测会员账号存在
     * @param   bool    $updateUser 是否在注册完成后更新当前用户的session信息
     *
     * @return int  返回注册的会员id
     */
	public function userReg( $data ,$flag = true,$updateUser = false){
    	if($flag){
		    $user = self::where("phone",$data['phone'])->field('uid')->find();
		    if ( !empty($user) ) {
			    $this->error = '会员号存在，请注册后登陆';
			    return false;
		    }
	    }
		$this->autoCompleteData( $this->insert );
		$this->password = $data['password'];
		$this->phone = $data['phone'];
		$this->open_id = $data['weixin'];
		$this->nickname = $data['phone'];
		if ($uid = self::insertGetId($this->data)) {
			if($updateUser){
				$userInfo = [
					'uid'       => $uid,
					'nickname'  => $data['phone'],
					'username'  => $this->data['username'],
					'phone'     =>  $this->data['phone'],
					'rid'       =>  $this->data['rid'],
					'open_id'   =>  $data['weixin']
				];
				session('user',$userInfo);
			}
			return $uid;
		}
	}





}
